/**
 * 
 */
package org.xelnaga.defiler.domain;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import edu.emory.mathcs.backport.java.util.Arrays;

/**
 * @author qijb
 *
 */
public class ContentletMapModel extends HashMap<String, Object>{
	
	@SuppressWarnings("unchecked")
	private static final List<String> systemVariable = Arrays.asList(new String[]{
			"title", "content", "inode", "friendlyName", "showOnMenu"
	});
	
	private List<FieldWrapper> fieldWrappers = new ArrayList<FieldWrapper>();
	/**
	 * 
	 */
	private static final long serialVersionUID = -1264623386701172996L;

	public ContentletMapModel() {
		
	}
	public ContentletMapModel(List<FieldModel> fields, ContentletModel contentlet) {
		for(String string : systemVariable) {
			Object value;
			try {
				value = BeanUtils.getProperty(contentlet, string);
				this.put(string, value);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		if(null != fields && null != contentlet) {
			for(FieldModel field : fields) {
				if(StringUtils.isNotBlank(field.getFieldContentlet())) {
					try {
						Object value = BeanUtils.getProperty(contentlet, field.getFieldContentlet());
						this.put(field.getVelocityVarName(), value);
						fieldWrappers.add(new FieldWrapper(field, value));
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						e.printStackTrace();
					}
				}
			}
		}
		this.put("fieldWrappers", this.getFieldWrappers());
	}

	public List<FieldWrapper> getFieldWrappers() {
		return fieldWrappers;
	}

	
}
